package com.cysd.ewmksbs.ui.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.cszy.ewmxns.base.BaseVMActivity
import com.cszy.ewmxns.ext.launch
import com.cszy.ewmxns.ext.toast

import com.cysd.ewmksbs.ksb_utils.SpUtils
import com.cysd.ewmksbs.viewmodel.MainViewModel
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.csylhl.ewmksb.databinding.ActivityCreatQrCodeBinding
import com.hnyyac.ad.mds.IAdToActivityListener


class Ksb_CreatQrCodeActivity : BaseVMActivity<MainViewModel, ActivityCreatQrCodeBinding>(),IAdViewListener {
    private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    var app_process: String? = "0" //整个应用流程

    val type: Int by lazy {
        intent.getIntExtra("type", 0)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.headTitle.headCenterTitle.text = mViewModel.getTitleAndHint(type).title
        mBinding.evContent.hint = mViewModel.getTitleAndHint(type).hint
        app_process = SpUtils.getInstance().getString(BaseAppConstans.APP_PROCESS)
    }

    /**
     * 写一个启动自己的方法 而且带参数type
     */
    /**
     * 写一个启动自己的方法 而且带参数type 每次启动进来会谈激励广告
     */
    companion object {
        fun launch(context: Context,type : Int = 0) {
            var hasStarted = false
            fun startAc() {
                if (hasStarted) return
                hasStarted = true
                val bundle = Bundle()
                bundle.putInt("type", type)
                context.launch(bundle, Ksb_CreatQrCodeActivity::class.java)
            }

            var app_process = SpUtils.getInstance().getString(BaseAppConstans.APP_PROCESS)
            if (app_process == "0") {
                startAc()
                return
            }

            AdManager.loadVideoAndJumpAc(context as Activity, 5, object : IAdToActivityListener {
                override fun onAdToActivity() {
                    startAc()
                }

            })
        }
    }

    override fun initData() {
        AdManager.loadChapin_nocache(
            this,
            2,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    newGromoreFeedAd = NewGroMoreFeedAdManager(this@Ksb_CreatQrCodeActivity, BaseAdConstants.FEEDAD_Id, mBinding?.fragmentContainer,22)
                    newGromoreFeedAd?.loadAd()
                }
            })
    }

    override fun initListener() {
        mBinding.headTitle.headBack.setOnClickListener {
            finish()
        }
       mBinding.main.setOnClickListener {
            hideKeyboard(mBinding.main)
       }
        mBinding.btnCreat.setOnClickListener {


            val content = mBinding.evContent.text.toString()
            if (content.isEmpty()) {
                toast("请输入内容")

            }else{
                if (app_process == "0"){//黑名单界面
                    var bundle=Bundle()
                    bundle.putString("content",content)
                    launch(bundle,Ksb_CreatQrCodeResultActivity::class.java)
                    finish()
                }else{
                    var bundle=Bundle()
                    bundle.putString("content",content)
                    launch(bundle,Ksb_CreatQrcodeProGressActivity::class.java)
                    finish()
                }

            }
        }
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        newGromoreFeedAd = NewGroMoreFeedAdManager(this@Ksb_CreatQrCodeActivity, BaseAdConstants.FEEDAD_Id, mBinding?.fragmentContainer,22)
        newGromoreFeedAd?.loadAd()
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
    fun hideKeyboard(view: View) {
        val inputMethodManager = view.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }

}